package com.guli.common.pojo.vo;

import com.guli.common.pojo.vo.SkuItemVo;
import lombok.Setter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/*
 * @Author 罗俊
 * @date 2020/11/8 - 8:43 上午
 *
 */

/**
 * 需要计算的属性，必须重写它的get()方法，保证每次获取的时候都会进行计算
 */
@Setter
public class CartVo {

    /**
     * 整个购物车中的商品
     */
    private List<? extends SkuItemVo> items;

    /**
     * 购物车中所有商品的数量（每件商品的数量相加）
     */
    private Integer countNumber;

    /**
     * 商品类型的数量
     */
    private Integer countType;

    /**
     * 商品总价
     */
    private BigDecimal totalAmount;

    /**
     * 折扣（减免的价格）
     */
    private BigDecimal reduce = new BigDecimal(0);


    public List<? extends SkuItemVo> getItems() {
        return items;
    }

    public Integer getCountNumber() {
        this.countNumber = 0;
        if (!CollectionUtils.isEmpty(this.items)) {
            for (SkuItemVo item : this.items) {
                this.countNumber += item.getCount();
            }
            return countNumber;
        }
        return 0;
    }

    public Integer getCountType() {

        if (ObjectUtils.isEmpty(this.items)) {

            this.countType = 0;
            return this.countType;
        }
        this.countType = this.items.size();

        return countType;
    }

    public BigDecimal getTotalAmount() {
        this.totalAmount = new BigDecimal(0);
        if (!CollectionUtils.isEmpty(this.items)) {
            for (SkuItemVo item : this.items) {
                this.totalAmount = this.totalAmount.add(item.getTotalPrice());
            }
            this.totalAmount = this.totalAmount.subtract(this.getReduce());
            return totalAmount;
        }
        return totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }
}
